<template>
  <div class="cofirmorder">
    <div class="cofirmorder__content">
      <div class="cofirmorder__content__address"
           @click="toAddressPage">
        <div class="cofirmorder__content__address__content"
             v-if="Object.keys(addressModel).length>0">
          <div class="cofirmorder__content__address__content__icon">
            <img src="@image/qrdd-location.png">
          </div>
          <div class="cofirmorder__content__address__content__item">
            <div class="cofirmorder__content__address__content__item__row1">
              <div>
                <span>收货人：{{ addressModel.recipients }}</span>
              </div>
              <div>
                <span>{{ addressModel.cellphone }}</span>
              </div>
            </div>
            <div class="cofirmorder__content__address__content__item__row2">
              <span>收货地址：{{ addressModel.province+addressModel.city+addressModel.area+addressModel.adress
                }}
              </span>
            </div>
          </div>
          <div class="cofirmorder__content__address__content__detail">
            <img src="@image/date-right.png">
          </div>
        </div>
        <div class="cofirmorder__content__address-empty"
             v-else>
          <div class="cofirmorder__content__address-empty__icon">
            <img src="@image/qrdd-location.png">
          </div>
          <div class="cofirmorder__content__address-empty__item">
            <div class="cofirmorder__content__address-empty__item__row1">

              <span>请选择收货地址</span>

            </div>
          </div>
        </div>
        <div class="cofirmorder__content__address__seperate" />
      </div>
      <!-- <cofirmorder-section v-for="(item,index) in cartData.confirmlist"
                           :key="index"
                           :index="index"
                           :model="item" /> -->
      <cofirmorder-cell v-for="(item ,index) in cartData.confirmlist"
                        :index="index"
                        :model="item"
                        :discount="cartData.discount"
                        :key="index"></cofirmorder-cell>
      <cofirmorder-footer :newModel="totalModel"
                          :params="params"></cofirmorder-footer>
    </div>
    <div class="cofirmorder__footer">
      <div class="cofirmorder__footer__line" />

      <div class="cofirmorder__footer__line" />

      <div class="cofirmorder__footer__line" />
      <div class="cofirmorder__footer__btn">
        <button class="btn__n"
                @click="confirmOrder">提交订单</button>
      </div>
    </div>
  </div>
</template>

<script>
import {isEmptyObject, calc} from '@/_util'
import cofirmorderCell from './confirm-order-cell/cofirmorder-cell'
import cofirmorderFooter from './confirm-order-cell/cofirmorder-footer'
import mixins from '@/mixins'
export default {
  name: 'cofirmorder',
  mixins: [mixins],
  data () {
    return {
      addressModel: {},
      /**
       *  1.用于表示是否显示弹出显示继续购买剩余的
       *  2.在获取请求参数的时候需要判断是否是直接取得错误返回的pds了还是从effective 中获取
       */
      isErrorAlert: false,
      /// 用于记录确认订单错误时，返回的结果
      confirmResponse: {},
      requestNetParams: {
        list: [], // psid,cartnumber,cartids(购物车)
        iscart: '0' // iscart: 1.购物车，0: 立刻订购
      },
      cartData: {},
      totalModel: {},
      params: {
        message: ''
      }
    }
  },
  async mounted () {
    // this.wxShare()
    this.initParams()
    await this.requestNet()
    this.initAddress()
    this.initTotal()
  },
  components: {
    cofirmorderCell,
    cofirmorderFooter
  },
  methods: {
    initAddress () {
      let storeItem = this.$cache.getCache(this.$cache.keys.addressOrder)
      if (storeItem && !isEmptyObject(storeItem)) {
        this.addressModel = JSON.parse(storeItem)
      } else {
        this.addressModel = this.cartData.address || {}
      }
    },
    initTotal () {
      let numTotal = 0
      let priceTotal = 0
      if (this.cartData && this.cartData.confirmlist) {
        for (let j = 0; j < this.cartData.confirmlist.length; j++) {
          if (this.cartData.confirmlist && this.cartData.confirmlist[j].skulist) {
            for (let i = 0; i < this.cartData.confirmlist[j].skulist.length; i++) {
              let sku = this.cartData.confirmlist[j].skulist[i]
              numTotal = calc(numTotal).add(sku.detailrnumber).over()
              priceTotal = calc(priceTotal).add(sku.detailtotal).over()
            }
          }
        }
      }
      this.totalModel = {
        numTotal: numTotal,
        priceTotal: calc(priceTotal).mul(this.cartData.discount).round(2, 1).over()
      }
    },
    initParams () {
      // iscart: 1.购物车，0: 立刻订购
      this.requestNetParams.iscart = this.$route.query.iscart || '0'
      this.requestNetParams.list = JSON.parse(this.$route.query.list)
      // console.error(this.requestNetParams)
    },
    toAddressPage () {
      if (this.addressModel && !isEmptyObject(this.addressModel)) {
        this.$router.push({
          name: 'address-manager',
          query: {
            type: '1',
            addid: this.addressModel.addid
          }
        })
      } else {
        this.$router.push({
          name: 'add-address',
          query: {
            isAdd: true
          }
        })
      }
    },
    async requestNet () {
      this.cartData = await this.$ajax.post('/tCart/confirmList.json', this.requestNetParams)
      this.addressModel = this.cartData.address
    },
    async confirmOrder () {
      if (isEmptyObject(this.addressModel)) {
        this.$toast('请选择收货地址')
        return
      }

      let params = {
        addid: this.addressModel.addid,
        paytype: '0',
        list: this.requestNetParams.list,
        leaveword: this.params.message
      }
      console.error(this.params)
      let order = await this.$ajax.post('/tOrder/addOrder.json', {
        ...params
      })
      order = order.order
      this.$router.push({
        name: 'pay',
        query: {
          orderitem: JSON.stringify({
            orderamount: order.orderamount,
            orderid: order.orderid
          })
        }})
    },
    confirmClick (pds) {
      let param = this.createorderParam(pds)
      if (isEmptyObject(param)) {
        // return
      }
    }
  },
  destroyed () {
    this.$cache.removeCache(this.$cache.keys.addressOrder)
  }
}
</script>

<style lang="stylus" scoped>
.teset {
  width: 100%;
  height: 45px;
}

.cofirmorder {
  position: absolute;
  bottom: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  font-size: 26px;

  .cofirmorder__content {
    overflow-y: scroll;
    flex: 1;
    width: 100%;
    z-index: 100;

    .cofirmorder__content__address {
      .cofirmorder__content__address__content {
        display: flex;
        justify-content: center;
        flex-direction: row;
        align-items: center;
        padding-left: 20px;
        padding-right: 20px;
        display: flex;
        justify-content: center;
        flex-direction: row;

        img {
          width: 100%;
          height: 100%;
        }

        .cofirmorder__content__address__content__icon {
          width: 34px;
          heigth: 34px;
        }

        .cofirmorder__content__address__content__item {
          flex: 1;
          display: flex;
          align-items: flex-start;
          justify-content: center;
          flex-direction: column;
          padding-left: 16px;
          padding-right: 20px;

          .cofirmorder__content__address__content__item__row1 {
            height: 60px;
            width: 100%;
            align-items: center;
            display: flex;
            justify-content: space-between;
            flex-direction: row;
            font-size: 28px;
          }

          .cofirmorder__content__address__content__item__row2 {
            height: 60px;
            width: 100%;
            align-items: center;
            display: flex;
            justify-content: flex-start;
            flex-direction: row;
          }
        }

        .cofirmorder__content__address__content__detail {
          width: 18px;
          height: 30px;
        }
      }

      .cofirmorder__content__address-empty {
        width: 100%;
        height: 124px;
        align-items: center;
        padding-left: 20px;
        padding-right: 20px;
        display: flex;

        img {
          width: 100%;
          height: 100%;
        }

        .cofirmorder__content__address-empty__icon {
          width: 34px;
          height: 34px;
        }

        .cofirmorder__content__address-empty__item {
          flex: 1;
          display: flex;
          align-items: flex-start;
          justify-content: center;
          flex-direction: column;
          padding-left: 16px;
          padding-right: 20px;

          .cofirmorder__content__address-empty__item__row1 {
            height: 60px;
            width: 100%;
            align-items: center;
            display: flex;
            justify-content: space-between;
            flex-direction: row;
          }
        }
      }

      .cofirmorder__content__address__seperate {
        width: 100%;
        background-color: #f8f8f8;
        height: 15px;
      }
    }

    .cofirmorder__content__row {
      width: 100%;
      margin-top: 10px;
      margin-bottom: 10px;
      height: 67px;
      background: red;
    }
  }

  .cofirmorder__footer {
    width: 100%;

    .cofirmorder__footer__line {
      width: 100%;
      height: 1px;
      background: $separate-line;
    }

    .cofirmorder__footer__btn {
      padding-top: 14px;
      padding-bottom: 14px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
